/**
 * 六合彩的逻辑
 */
(function() {
    var currTabId = 1;
    var tabDraws = [
        // 1：特码
        function() {
            fillTeMa();
        },

        // 2 两面
        function() {
            fillLM();
        },

        // 3 色波
        function() {
            fillSeBo();
        },

        // 4 特效
        function() {
            fillTeXiao();
        },

        // 5 合肖
        function() {
            fillHeXiao();
        },

        // 6 特码头尾数
        function() {
            fillTeMaTWS();
        },

        // 7 正码
        function() {
            fillZhengMa();
        },

        // 8 正码特
        function() {
            fillZMT();
        },

        // 9 正码1-6
        function() {
            fillZM6();
        },
        
        // 10 五行
        function() {
            fillWuXing();
        },

        // 11 一肖
        function() {
            fillYiXiao();
        },

        // 12 正肖
        function() {
            fillZhengXiao();
        },
        
        // 13 七色波
        function() {
            fillQisebo();
        },

        // 14 总肖
        function() {
            fillZongXiao();
        },

        // 15 自选不中
        function() {
            fillBuZhong();
        },

        // 16 连肖
        function() {
            fillLianXiao();
        },

        // 17 连码
        function() {
            fill_lianma();
        }
    ];



    function fillTeMa() {
        // 特码49个号码
        var table = $("#PAGE_A tbody");
        table.empty();
        for (var i = 1; i <= 10; i++) {
            var tr = $("<tr></tr>");
            for (var j = i; j <= i + 40; j += 10) {
                var k = j < 10 ? "0" + j : j;
                if (j > 49) {
                    tr.append("<td colspan='3'> </td>");
                    break;
                }
                add_bet_item('特码' + k, draw_number(j), tr);
            }
            table.append(tr);
        }

        var table = $("#PAGE_B tbody");
        table.empty();
        for (var i = 1; i <= 10; i++) {
            var tr = $("<tr></tr>");
            for (var j = i; j <= i + 40; j += 10) {
                var k = j < 10 ? "0" + j : j;
                if (j > 49) {
                    tr.append("<td colspan='3'> </td>");
                    continue;
                }
                add_bet_item('特码B' + k, draw_number(j), tr);
            }
            table.append(tr);
        }

        // 大小单双(特码页面)
        var arr = ['大', '单', '合大', '合单', '小', '双', '合小', '合双', '尾大', '大单', '小单', '尾小', '大双', '小双'];
        var table = $(".tema tbody");
        table.empty();
        var f = function(start, count, table) {
            var tr = $("<tr></tr>");
            for (var i = start; i < start + count; i++) {
                add_bet_item('特' + arr[i], '特' + arr[i], tr);
            }
            if (count < 4) tr.append("<td colspan='3'> </td>");
            table.append(tr);
        }
        f(0, 4, table); f(4, 4, table);
        f(8, 3, table); f(11, 3, table);
    }

    function fillLM() {
        // 两面盘
        var arr = ['大', '小', '单', '双', 
            '合大', '合小', '合单', '合双', 
            '天肖', '地肖', '前肖', '后肖', '家肖', '野肖', 
            '大尾', '小尾', ];
        var table = $(".lmp tbody");
        table.empty();
        var tr = $("<tr></tr>");
        for (var i in arr) {
            var k = arr[i];
            if (k[1] == '肖') k = k[0];
            if (k == '大尾') k = '尾大';
            if (k == '小尾') k = '尾小';
            add_bet_item('特' + k, '特' + arr[i], tr);
            if (i % 2 == 1) {
                table.append(tr);
                tr = $("<tr></tr>");
            }
        }
        var tr = $("<tr></tr>");
        add_bet_item('和单', '总和单', tr);
        add_bet_item('和双', '总和双', tr);
        table.append(tr);

        var tr = $("<tr></tr>");
        add_bet_item('和大', '总和大', tr);
        add_bet_item('和小', '总和小', tr);
        table.append(tr);

        var tr = $("<tr></tr>");
        add_bet_item('特大单', '特大单', tr);
        add_bet_item('特小单', '特小单', tr);
        table.append(tr);

        var tr = $("<tr></tr>");
        add_bet_item('特大双', '特大双', tr);
        add_bet_item('特小双', '特小双', tr);
        table.append(tr);
    }

    function fillSeBo() {
        // 色波
        var table = $(".sebo tbody");
        table.empty();
        var f = function(numbers, k, name, table) {
            var tr = $("<tr></tr>");
            tr.append("<td>" + name + "</td>");
            var td = $("<td class='qiu'></td>");
            var l = '<i class="lhc_icon lhc_b__i__ mr1">__i__</i>';
            for (var i in numbers) {
                td.append(l.replace(/__i__/g, numbers[i] < 10 ? '0' + numbers[i] : numbers[i]));
            }
            tr.append(td);

            add_bet_item(k, name, tr, true);
            table.append(tr);
        };
        f(gameRule.hongbo(), '特红', '红波', table);
        f(gameRule.lanbo(), '特蓝', '蓝波', table);
        f(gameRule.lvbo(), '特绿', '绿波', table);

        // 半波
        var arr = ['单', '双', '大', '小'];
        var arr2 = ['红', '蓝', '绿'];
        var table = $(".banbo tbody");
        table.empty();
        for (var j in arr) {
            var tr = $("<tr></tr>");
            for (var i in arr2) {
                var k = '特' + arr2[i] + arr[j];
                add_bet_item(k, arr2[i] + arr[j], tr);
            }
            table.append(tr);
        }

        // 半半波
        var arr = ['大单', '大双', '小单', '小双'];
        var table = $(".banbanbo tbody");
        table.empty();
        for (var j in arr) {
            var tr = $("<tr></tr>");
            for (var i in arr2) {
                var k = '特' + arr2[i] + arr[j];
                add_bet_item(k, arr2[i] + arr[j], tr);
            }
            table.append(tr);
        }
    }

    function fillTeXiao() {
        // 特肖
        var table = $(".texiao");
        table.empty();
        var arr = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'];
        var tr = $("<tr></tr>");
        for (var i in arr) {
            tr.append('<td class="name">' + arr[i] + '</td>');
            var td = $('<td class="qiu"></td>');
            tr.append(td);

            var numbers = gameRule.getXiaoNumbers(arr[i]);
            for (var z in numbers) {
                var n = numbers[z] < 10 ? '0' + numbers[z] : numbers[z]; 
                td.append('<i class="lhc_icon lhc_b__i__ mr2">__i__</i>'.replace(/__i__/g, n));
            }

            add_bet_item('特' + arr[i], arr[i], tr, true);
            if (i % 2 == 1) {
                table.append(tr);
                tr = $("<tr></tr>");
            }
        }
    }

    function fillTeMaTWS() {
        // 特码头尾数
        var table = $(".touweishu tbody")
        table.empty();
        for (var i = 0; i <= 4; i++) {
            var tr = $("<tr></tr>");
            add_bet_item('特头' + i, i + '头', tr);
            add_bet_item('特尾' + (i + 1), (i + 1) + '尾', tr);
            if (i + 6 < 10)
                add_bet_item('特尾' + (i + 6), (i + 6) + '尾', tr);
            else
                add_bet_item('特尾0', '0尾', tr);
            table.append(tr);
        }
    }

    function fillZhengMa() {
        // 正码
        var table = $(".zhengma tbody")
        table.empty();
        for (var i = 1; i <= 10; i++) {
            var tr = $("<tr></tr>");
            for (var j = i; j <= i + 40; j += 10) {
                if (j > 49) {
                    tr.append("<td colspan='3'> </td>");
                    break;
                }
                add_bet_item("正" + j, draw_number(j), tr);
            }
            table.append(tr);
        }

        var tr = $(".zonghe tbody tr");
        tr.empty();
        add_bet_item('和大', '总大', tr);
        add_bet_item('和小', '总小', tr);
        add_bet_item('和单', '总单', tr);
        add_bet_item('和双', '总双', tr);
    }

    function fillZMT() {
        // 正一特~正六特
        var arr = ['一', '二', '三', '四', '五', '六'];
        for (var z = 1; z <= 6; z++) {
            var table = $("#ZMT_PAGE" + z + " tbody");
            table.empty();
            for (var i = 1; i <= 10; i++) {
                var tr = $("<tr></tr>");
                for (var j = i; j <= i + 40; j += 10) {
                    var k = j < 10 ? "0" + j : j;
                    if (j > 49) {
                        tr.append("<td colspan='3'> </td>");
                        break;
                    }
                    add_bet_item(arr[z - 1] + '码' + k, draw_number(j), tr);
                }
                table.append(tr);
            }
        }
        
        var arr2 = ['大', '单', '合单', '小', '双', '合双', '红', '蓝', '绿'];
        for (var z = 1; z <= 6; z++) {
            var table = $("#ZT_LMP" + z + " tbody");
            table.empty();
            var tr = $("<tr></tr>");
            for (var j in arr2) {
                add_bet_item(arr[i] + arr2[j], '正' + (z) + '-' + arr2[j], tr);
                if (j % 3 == 2) {
                    table.append(tr);
                    tr = $("<tr></tr>");
                }
            }
        }
    }

    function fillZM6() {
        // 正码1~6
        var arr = ['一', '二', '三', '四', '五', '六'];
        var arr2 = ['单', '双', '大', '小', '合单', '合双', '合大', '合小', '红', '蓝', '绿', '尾大', '尾小'];
        var div = $(".q1-3 tbody");
        div.empty();
        for (var i in arr2) {
            var tr = $("<tr></tr>");
            for (var z = 0; z < 3; z++) {
                var name = arr2[i];
                if (i < 4) name += '码';
                if (name == '红' || name == '蓝' || name == '绿') name += '波';
                add_bet_item(arr[z] + arr2[i], name, tr); 
            }
            div.append(tr);
        }
        var div = $(".q4-6 tbody");
        div.empty();
        for (var i in arr2) {
            var tr = $("<tr></tr>");
            for (var z = 3; z < 6; z++) {
                var name = arr2[i];
                if (i < 4) name += '码';
                if (name == '红' || name == '蓝' || name == '绿') name += '波';
                add_bet_item(arr[z] + arr2[i], name, tr); 
            }
            div.append(tr);
        }
    }

    function fillWuXing() {
        // 五行
        var table = $(".wuxing tbody");
        table.empty();
        var f = function(numbers, k, name, table) {
            var tr = $("<tr></tr>");
            tr.append("<td>" + name + "</td>");
            var td = $("<td class='qiu'></td>");
            var l = '<i class="lhc_icon lhc_b__i__ mr1">__i__</i>';
            for (var i in numbers) {
                td.append(l.replace(/__i__/g, numbers[i] < 10 ? '0' + numbers[i] : numbers[i]));
            }
            tr.append(td);

            add_bet_item(k, name, tr, true);
            table.append(tr);
        };
        
        f(gameRule.jin(), '特金', '金', table);
        f(gameRule.mu(), '特木', '木', table);
        f(gameRule.shui(), '特水', '水', table);
        f(gameRule.huo(), '特火', '火', table);
        f(gameRule.tu(), '特土', '土', table);
    }

    function fillYiXiao() {
        // 一肖
        var table = $("#PAGE_YIXIAO tbody");
        table.empty();
        var arr = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'];
        for (var j = 0; j < 6; j++) {
            var tr = $("<tr></tr>");
            for (var i = j; i <= j + 6; i += 6) {
                tr.append('<td class="name">' + arr[i] + '</td>');
                var numbers = gameRule.getXiaoNumbers(arr[i]);
                var td = $("<td class='qiu'></td>");
                for (var z in numbers) td.append(draw_number(numbers[z]));
                tr.append(td);

                add_bet_item('平特' + arr[i], arr[i], tr, true);
            }
            table.append(tr);
        }

        // 尾数
        var table = $("#PAGE_WEISHU tbody");
        table.empty();
        for (var j = 0; j < 5; j++) {
            var tr = $("<tr></tr>");
            for (var i = j; i <= j + 5; i += 5) {
                tr.append("<td class='name'>" + i + "尾</td>");
                var td = $("<td class='qiu'></td>"); tr.append(td);
                for (var z = 1; z <= 49; z++) {
                    if (z % 10 == i) {
                        td.append(draw_number(z));
                    }
                }

                add_bet_item('平特尾' + i, "平特" + i + '尾', tr, true);
            }
            table.append(tr);    
        }
    }

    function fillZhengXiao() {
        // 正肖
        var table = $(".zhengxiao tbody");
        table.empty();
        var arr = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'];
        for (var j = 0; j < 6; j++) {
            var tr = $("<tr></tr>");
            for (var i = j; i <= j + 6; i += 6) {
                tr.append('<td class="name">' + arr[i] + '</td>');
                var numbers = gameRule.getXiaoNumbers(arr[i]);
                var td = $("<td class='qiu'></td>");
                for (var z in numbers) td.append(draw_number(numbers[z]));
                tr.append(td);

                add_bet_item('正' + arr[i], arr[i], tr, true);
            }
            table.append(tr);
        }
    }

    function fillQisebo() {
        // 七色波
        var table = $(".qisebo tbody");
        table.empty();
        var tr = $("<tr></tr>");
        add_bet_item('七红', '红波', tr); 
        add_bet_item('七蓝', '蓝波', tr); 
        table.append(tr);

        var tr = $("<tr></tr>");
        add_bet_item('七绿', '绿波', tr); 
        add_bet_item('七和', '和局', tr);
        table.append(tr);
    }

    function fillZongXiao() {
        // 总肖
        var table = $(".zongxiao tbody");
        table.empty;
        var tr = $("<tr></tr>");
        for (var i = 2; i <= 7; i++) {
            add_bet_item('总' + i, i + '肖', tr);
            if (i == 4) {
                table.append(tr);
                tr = $("<tr></tr>");
            }
        }
        table.append(tr);
        var tr = $("<tr></tr>");
        add_bet_item('总单', '总肖单', tr);
        add_bet_item('总双', '总肖双', tr);
        tr.append("<td colspan='3'></td>");
        table.append(tr);
    }

    function draw_number(n) {
        var k = n < 10 ? '0' + n : n;
        return '<i class="lhc_icon lhc_b__i__">__i__</i>'.replace(/__i__/g, k);
    }

    /************ 合肖的处理start */
    function fillHeXiao() {
        var arr = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'];
        var table = $("#hexiao_checkboxs tbody");
        table.empty();
        for (var j = 0; j < 6; j++) {
            var tr = $("<tr></tr>");
            for (var i = j; i <= j + 6; i += 6) {
                tr.append('<td class="name">' + arr[i] + '</td>');
                var numbers = gameRule.getXiaoNumbers(arr[i]);
                var td = $("<td class='qiu'></td>");
                for (var z in numbers) td.append(draw_number(numbers[z]));
                tr.append(td);
                tr.append('<td class="checkbox_td"><input type="checkbox" value="__v__"></td>'.replace(/__v__/, arr[i]));
            }
            table.append(tr);
        }
        $("#hexiao_checkboxs td.checkbox_td input").change(function() {
            refresh_hexiao_numbers();
        });
    }

    function refresh_hexiao_numbers() {
        var arr = $("#hexiao_checkboxs input");
        var list = get_hexiao_numbers();
        var max = 11;

        arr.attr("disabled", false);
        if (list.length >= max) {
            for (var i = 0; i < arr.length; i++) {
                if (!arr[i].checked)
                    $(arr[i]).attr("disabled", true);
            }
        }

        for (var i = 0; i < arr.length; i++) {
            if (arr[i].checked) {
                $(arr[i]).parent().attr("class", "checkbox_td bg_yellow");
            }
            else {
                $(arr[i]).parent().attr("class", "checkbox_td");
            }
        }

        // 设置赔率
        var n = list.length;
        if (n < 2) {
            $("#hexiao_odds").html("--");
        }
        else {
            var k = get_hexiao_target();
            var rate = gameRule.map[k] ? gameRule.map[k].rate : "";
            $("#hexiao_odds").html(rate);
        }
    }

    function get_hexiao_target() {
        var list = get_hexiao_numbers();
        if (list.length < 2) return null;
        return '合' + list.length;
    }

    function get_hexiao_numbers() {
        var arr = $("#hexiao_checkboxs input:checked");
        var list = [];
        for (var i = 0; i < arr.length; i++) {
            list.push($(arr[i]).attr("value"))
        }
        return list;
    }

    function clear_hexiao() {
        var arr = $("#hexiao_checkboxs input:checked");
        for (var i = 0; i < arr.length; i++) $(arr[i])[0].checked = false;
        refresh_hexiao_numbers();
    }
    /************ 合肖的处理end */

    /************ 自选不中的处理start */
    function fillBuZhong() {
        var table = $("#zxbz_checkboxs tbody");
        table.empty();
        for (var z = 1; z <= 10; z++) {
            var tr = $("<tr></tr>");
            for (var i = z; i <= z + 40; i += 10) {
                if (i == 50) {
                    tr.append('<td colspan="2"></td>');
                    continue;
                }

                tr.append('<td class="name not-event">' + draw_number(i) + '</td>');
                tr.append('<td class="checkbox_td"><input type="checkbox" value="__i__"></td>'.replace(/__i__/, i));
            }
            table.append(tr);
        }

        $("#zxbz_checkboxs input").change(function() {
            refresh_buzhong_numbers();
        });
    }

    function refresh_buzhong_numbers() {
        var arr = $("#zxbz_checkboxs input");
        var list = get_buzhong_numbers();
        var max = 12;

        arr.attr("disabled", false);
        if (list.length >= max) {
            for (var i = 0; i < arr.length; i++) {
                if (!arr[i].checked)
                    $(arr[i]).attr("disabled", true);
            }
        }

        for (var i = 0; i < arr.length; i++) {
            if (arr[i].checked) {
                $(arr[i]).parent().attr("class", "checkbox_td bg_yellow");
            }
            else {
                $(arr[i]).parent().attr("class", "checkbox_td");
            }
        }

        // 设置赔率
        var n = list.length;
        if (n < 5) {
            $("#zxbz_odds").html("");
        }
        else {
            var k = get_buzhong_target();
            var rate = gameRule.map[k] ? gameRule.map[k].rate : "";
            $("#zxbz_odds").html(rate);
        }
    }

    function get_buzhong_target() {
        var list = get_buzhong_numbers();
        if (list.length < 5) return null;
        return '不中' + list.length;
    }

    function get_buzhong_numbers() {
        var arr = $("#zxbz_checkboxs input:checked");
        var list = [];
        for (var i = 0; i < arr.length; i++) {
            list.push($(arr[i]).attr("value"))
        }
        return list;
    }

    function clear_buzhong() {
        var arr = $("#zxbz_checkboxs input:checked");
        for (var i = 0; i < arr.length; i++) $(arr[i])[0].checked = false;
        refresh_buzhong_numbers();
    }
    /************ 自选不中的处理end */


    /********** 连肖连尾处理start */
    function fillLianXiao() {
        var types = ['连肖2', '连肖3', "连肖4", "连肖5", "连尾2", "连尾3", "连尾4", "连尾5"];
        var arr = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'];
        for (var z = 1; z <= 4; z++) {
            var table = $("#LIANXIAO_PAGE" + z + " tbody");
            table.empty();

            for (var j = 0; j < 6; j++) {
                var tr = $("<tr></tr>");
                for (var i = j; i <= j + 6; i += 6) {
                    tr.append('<td class="name">' + arr[i] + '</td>');
                    var numbers = gameRule.getXiaoNumbers(arr[i]);
                    var td = $("<td class='qiu'></td>");
                    for (var k in numbers) td.append(draw_number(numbers[k]));
                    tr.append(td);

                    // 赔率
                    var key = types[z - 1] + arr[i];
                    var rate = gameRule.map[key] ? gameRule.map[key].rate : "";
                    var l = '<td class="odds not-event">' + 
                            '  <span class="special-rate c-txt3" key="__k__">__rate__</span></td>';
                    l = l.replace(/__k__/, key);
                    l = l.replace(/__rate__/, rate);
                    tr.append(l);

                    tr.append('<td class="checkbox_td"><input type="checkbox" value="__v__"></td>'.replace(/__v__/, arr[i]));
                }
                table.append(tr);
            }
        }
        
        for (var z = 5; z <= 8; z++) {
            var table = $("#LIANXIAO_PAGE" + z + " tbody");
            table.empty();

            for (var j = 0; j < 5; j++) {
                var tr = $("<tr></tr>");
                for (var i = j; i <= j + 5; i += 5) {
                    tr.append('<td class="name">' + i + '尾</td>');
                    var td = $("<td class='qiu'></td>");
                    for (var k = 1; k <= 49; k++) {
                        if (k % 10 == i) {
                            td.append(draw_number(k));
                        }
                    }
                    tr.append(td);

                    // 赔率
                    var key = types[z - 1] + i;
                    var rate = gameRule.map[key] ? gameRule.map[key].rate : "";
                    var l = '<td class="odds not-event">' + 
                            '  <span class="special-rate c-txt3" key="__k__">__rate__</span></td>';
                    l = l.replace(/__k__/, key);
                    l = l.replace(/__rate__/, rate);
                    tr.append(l);

                    tr.append('<td class="checkbox_td"><input type="checkbox" value="__v__"></td>'.replace(/__v__/, i));
                }
                table.append(tr);
            }
        }

        $("#tab16 .checkbox_td input").change(function() {
            refresh_lianxiao_numbers();
        });
    }

    // 当前的投注类型
    function get_lianxiao_target() {
        return $("#lxlw_tabs a.on").attr("value");
    }

    // 取得当前选中的数字列表
    function get_lianxiao_numbers() {
        var list = [];
        var arr = $("#tab16 input:checked");
        for (var i = 0; i < arr.length; i++) {
            list.push($(arr[i]).attr("value"))
        }
        return list;
    }

    // 清理选中的数字
    function clear_lianxiao() {
        var arr = $("#tab16 .checkbox_td input:checked");
        for (var i = 0; i < arr.length; i++) $(arr[i])[0].checked = false;
        refresh_lianxiao_numbers();
    }

    // 刷新连肖的数字信息
    function refresh_lianxiao_numbers() {
        var arr = $("#tab16 input");
        for (var i = 0; i < arr.length; i++) {
            if (arr[i].checked) {
                $(arr[i]).parent().attr("class", "checkbox_td bg_yellow");
            }
            else {
                $(arr[i]).parent().attr("class", "checkbox_td");
            }
        }
    }
    /********** 连肖连尾处理end */

    /*************处理连码 */
    // 填充连码
    function fill_lianma() {
        var table = $("#lianma_checkboxs");
        table.empty();
        for (var z = 1; z <= 10; z++) {
            var tr = $("<tr></tr>");
            for (var i = z; i <= z + 40; i += 10) {
                if (i == 50) {
                    tr.append('<td colspan="2"></td>');
                    break;
                }

                tr.append('<td class="name not-event">' + draw_number(i) + '</td>');
                tr.append('<td class="checkbox_td"><input type="checkbox" value="__i__"></td>'.replace(/__i__/, i));
            }
            table.append(tr);
        }

        $("#lianma_checkboxs input").change(function() {
            refresh_lianma_numbers();
        })
    }

    // 当前的投注类型
    function get_lianma_target() {
        return $("#lianma_radios input:checked").attr("value");
    }

    // 取得当前选中的数字列表
    function get_lianma_numbers() {
        var arr = $("#lianma_checkboxs input:checked");
        var list = [];
        for (var i = 0; i < arr.length; i++) {
            list.push($(arr[i]).attr("value"))
        }
        return list;
    }

    // 清理选中的数字
    function clear_lianma() {
        var arr = $("#lianma_checkboxs input:checked");
        for (var i = 0; i < arr.length; i++) $(arr[i])[0].checked = false;
        refresh_lianma_numbers();
    }

    // 刷新连码的数字信息
    function refresh_lianma_numbers() {
        var arr = $("#lianma_checkboxs input");
        var list = get_lianma_numbers();
        var k = get_lianma_target();
        var max = 7;
        if (k == '全3') max = 10;
        if (k == '全4') max = 4;

        arr.attr("disabled", false);
        if (list.length >= max) {
            // 其他数字需要置灰
            for (var i = 0; i < arr.length; i++) {
                if (!arr[i].checked)
                    $(arr[i]).attr("disabled", true);
            }
        }

        for (var i = 0; i < arr.length; i++) {
            if (arr[i].checked) {
                $(arr[i]).parent().attr("class", "checkbox_td bg_yellow");
            }
            else {
                $(arr[i]).parent().attr("class", "checkbox_td");
            }
        }
    }

    /********************** 处理连码结算 */

    var initF = function(game, first) {
        window.game = game;
        initGame(game, function() {
            if (false !== first)
                fillGameTabs(["特码", "两面", "色波", "特肖", "合肖", "特码头尾数", "正码", "正码特", "正码1~6", "五行",
                    "平特一肖尾数", "正肖", "7色波", "总肖", "自选不中", "连肖连尾", "连码"]);
            currTabId = parseInt($("#cate_menus_50 .selected").attr("tid"));
            $("#tab" + currTabId).attr("flag", 1);
            var f = tabDraws[currTabId - 1];
            f();
        }, first);

        $("#TAB_A").click(function() {
            $("#PAGE_A").show();
            $("#PAGE_B").hide();
            $("#TAB_A").attr("class", 'on');
            $("#TAB_B").attr("class", '');
        });
        $("#TAB_B").click(function() {
            $("#PAGE_B").show();
            $("#PAGE_A").hide();
            $("#TAB_B").attr("class", 'on');
            $("#TAB_A").attr("class", '');
        });
        $("#ztm_tabs a").click(function(e) {
            var a = $(e.currentTarget);
            if (a.attr("class") == 'on') return;
            reset_counter();
            var index = a.attr("value");
            $("#ztm_tabs a").removeClass("on");
            a.addClass("on");
    
            for (var i = 1; i <= 6; i++) {
                if (index == i) {
                    $("#ZMT_PAGE" + i).show();
                    $("#ZT_LMP" + i).show();
                }
                else {
                    $("#ZMT_PAGE" + i).hide();
                    $("#ZT_LMP" + i).hide();
                }
            }
        });
        $("#TAB_YIXIAO").click(function() {
            if ($("#TAB_YIXIAO").attr("class") == 'on') return;
            $("#TAB_YIXIAO").addClass("on");
            $("#TAB_WEISHU").removeClass("on");
            $("#PAGE_YIXIAO").show();
            $("#PAGE_WEISHU").hide();
            reset_counter();
        });
        $("#TAB_WEISHU").click(function() {
            if ($("#TAB_WEISHU").attr("class") == 'on') return;
            $("#TAB_WEISHU").addClass("on");
            $("#TAB_YIXIAO").removeClass("on");
            $("#PAGE_WEISHU").show();
            $("#PAGE_YIXIAO").hide();
            reset_counter();
        });
        $("#lxlw_tabs a").click(function(e) {
            var a = $(e.currentTarget);
            if (a.attr("class") == 'on') return;
            reset_counter();
            $("#lxlw_tabs a").removeClass('on');
            a.addClass('on');
    
            var type = a.attr('type');
            for (var i = 1; i <= 8; i++) {
                if (i == type) {
                    $("#LIANXIAO_PAGE" + i).show();
                }
                else
                    $("#LIANXIAO_PAGE" + i).hide();
            }
            reset_counter();
        });
        $("#lianma_radios input").change(function() {
            reset_counter();
        });
    }

    var resetF = function(f) {
        if ($(".cate_menu a.selected").attr("tid") == 5) {
            // 合肖
            clear_hexiao();
        }
        else if ($(".cate_menu a.selected").attr("tid") == 15) {
            // 自选不中
            clear_buzhong();
        }
        else if ($(".cate_menu a.selected").attr("tid") == 16) {
            // 连肖连尾
            clear_lianxiao();
        }
        else if ($(".cate_menu a.selected").attr("tid") == 17) {
            // 连码
            clear_lianma();
        }
        else
            return f();
    }

    var getCounterListF = function(f) {
        if ($(".cate_menu a.selected").attr("tid") == 5) {
            // 合肖
            var v = parseInt($("#bet_money1").val());
            if (v < 1) {
                layer.alert("下注内容不正确，请重新下注", { icon: 2 });
                return;
            }
            var list = get_hexiao_numbers();
            if (list.length < 2) {
                layer.alert("下注内容不正确，请重新下注", { icon: 2 });
                return;
            }

            var key = get_hexiao_target();
            key = key + ":" + list.join(",");
            var data = {};
            data[key] = v * 100;
            return data;
        }
        else if ($(".cate_menu a.selected").attr("tid") == 15) {
            // 自选不中
            var v = parseInt($("#bet_money1").val());
            if (v < 1) {
                layer.alert("下注内容不正确，请重新下注", { icon: 2 });
                return;
            }
            var list = get_buzhong_numbers();
            if (list.length < 5) {
                layer.alert("下注内容不正确，请重新下注", { icon: 2 });
                return;
            }

            var key = get_buzhong_target();
            key = key + ":" + list.join(",");
            var data = {};
            data[key] = v * 100;
            return data;
        }
        else if ($(".cate_menu a.selected").attr("tid") == 16) {
            // 连肖连尾
            var v = parseInt($("#bet_money1").val());
            if (v < 1) {
                layer.alert("下注内容不正确，请重新下注", { icon: 2 });
                return;
            }
            var key = get_lianxiao_target();
            var limit = parseInt(key[2]);
            var list = get_lianxiao_numbers();
            if (limit > list.length) {
                layer.alert("下注内容不正确，请重新下注", { icon: 2 });
                return;
            }

            var arr;
            if (limit == 2) arr = group2(list);
            else if (limit == 3) arr = group3(list);
            else if (limit == 4) arr = group4(list);
            else if (limit == 5) arr = group5(list);
            var data = {};
            for (var i in arr) {
                data[key + ":" + arr[i].join(",")] = v * 100;
            }
            return data;
        }
        else if ($(".cate_menu a.selected").attr("tid") == 17) {
            var v = parseInt($("#bet_money1").val());
            if (v < 1) {
                layer.alert("下注内容不正确，请重新下注", { icon: 2 });
                return;
            }
            var k = get_lianma_target();
            var list = get_lianma_numbers();
            var limit = 2;
            if (k != '特串') limit = parseInt(k[1]);
            if (v < 1 || list.length < limit) {
                layer.alert("下注内容不正确，请重新下注", { icon: 2 });
                return;
            }

            var key = get_lianma_target();
            key = key + ":" + list.join(",");
            var data = {};
            data[key] = v * 100;
            return data;
        }
        else 
            return f();
    }

    var parseTargetF = function(k) {
        var types = ['中3', '全3', '全2', '中2', '特串', '全4'];
        var arr = k.split(":");
        if (arr.length > 1 && types.indexOf(arr[0]) != -1) {
            var limit = 2;
            if (arr[0] != '特串') limit = parseInt(arr[0][1]);
            var list = arr[1].split(",");

            // 组数
            var betList = [];
            if (limit == 2) betList = group2(list);
            else if (limit == 3) betList = group3(list);
            else if (limit == 4) betList = group4(list);
            return gameRule.getTargetName(arr[0]) + " " + arr[1] + "[" + betList.length + "组]"
        }
        return gameRule.getTargetName(k);
    }

    var calcBetF = function(data) {
        var total = 0, count = 0;
        var types = ['中3', '全3', '全2', '中2', '特串', '全4'];
        for (var k in data) {
            var arr = k.split(":");
            if (arr.length > 1 && types.indexOf(arr[0]) != -1) {
                var list = arr[1].split(',');
                var limit = 2;
                if (arr[0] != '特串') limit = parseInt(arr[0][1]);

                // 组数
                var betList = [];
                if (limit == 2) betList = group2(list);
                else if (limit == 3) betList = group3(list);
                else if (limit == 4) betList = group4(list);
                count += betList.length;
                total += data[k] * betList.length;
                continue;
            }
            count++;
            total += data[k];
        }
        return [count, total];
    }

    var unpackF = function(data) {
        var r = {};
        var types = ['中3', '全3', '全2', '中2', '特串', '全4'];
        for (var k in data) {
            var arr = k.split(":");
            if (arr.length > 1 && types.indexOf(arr[0]) != -1) {
                var list = arr[1].split(',');
                var limit = 2;
                if (arr[0] != '特串') limit = parseInt(arr[0][1]);

                // 组数
                var betList = [];
                if (limit == 2) betList = group2(list);
                else if (limit == 3) betList = group3(list);
                else if (limit == 4) betList = group4(list);
                for (var i in betList) {
                    r[arr[0] + ':' + betList[i].join(",")] = data[k];
                }
                continue;
            }
            else
                r[k] = data[k];
        }
        return r;
    }

    // 绘制开奖结果
    var drawF = function(list) {
        var div = $("#result_balls");
        div.attr("class", "T_HK6 L_HK6");
        var l = '<span><b class="b__index__">__index__</b> <i>__sx__</i></span>';
        for (var i in list) {
            if (i == 6) {
                div.append("<span style='font-size:30px;font-weight:bold;'>+</span>");
            }
            var s = l.replace(/__index__/g, list[i]);
            s = s.replace(/__sx__/, gameRule.getShengXiao(parseInt(list[i])));
            div.append(s);
        }
    }

    var data = {
        drawResult: drawF,
        unpack: unpackF,
        calcBetList: calcBetF,
        parseTarget: parseTargetF,
        getCounterList: getCounterListF,
        reset: resetF,
        init: initF,
    }
    context.global.game["lhc"] = data;
})();    